﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace mamnontuoitho7.AppCode
{
    public class Message
    {
        public ConnectGetData _connectData;
        public Message()
        {
            _connectData = new ConnectGetData();
         }
        public void MessageCreate(String conversationID, String memberIDSend, String content, String isReadOwner, String isReadClass)
        {
            SqlConnection con = _connectData.GetConnect();

            try
            {
                con.Open();
                string sql = "insert [Message] values (@ConversationID, @MemberIDSend, @Content, default)";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@ConversationID", conversationID);
                cmd.Parameters.AddWithValue("@MemberIDSend", memberIDSend);
                cmd.Parameters.AddWithValue("@Content", content);
                
                cmd.ExecuteNonQuery();
                cmd.Dispose();

                string sql2 = "update [Conversation] set IsReadOwner = @IsReadOwner, IsReadClass = @IsReadClass where ConversationID = @ConversationID";
                SqlCommand cmd2 = new SqlCommand(sql2, con);
                cmd2.Parameters.AddWithValue("@IsReadOwner", isReadOwner);
                cmd2.Parameters.AddWithValue("@IsReadClass", isReadClass);

                cmd2.Parameters.AddWithValue("@ConversationID", conversationID);
                cmd2.ExecuteNonQuery();
                cmd2.Dispose();

                con.Close();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);

            }
        }

        /// <summary>
        /// check if the message belong to the current username
        /// </summary>
        /// <param name="username"></param>
        /// <param name="commentID"></param>
        /// <returns>true if the current user is the owner</returns>
        public bool checkMessageOwner(String messageID, String memberID)
        {
            bool flag = false;

            try
            {
                string sql = "select * from Message where MessageID = @MessageID and MemberIDSend =@MemberIDSend ";

                SqlCommand cmd = new SqlCommand(sql, _connectData.GetConnect());
                cmd.Parameters.AddWithValue("@MessageID", messageID);
                cmd.Parameters.AddWithValue("@MemberIDSend", memberID);
                DataTable dt = _connectData.GetDataTable(cmd);

                if (dt.Rows.Count > 0)
                {
                    flag = true;
                }
                cmd.Dispose();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Error error error: " + ex.StackTrace);
                return flag;
            }
            return flag;
        }


        /// <summary>
        /// get a number of record of a page
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="step"></param>
        /// <returns>datatable</returns>
        public DataTable GetMessagePaged(int pageIndex, int step, String ConversationID)
        {
            int startRowIndex = (pageIndex - 1) * step + 1;
            int endRowIndex = startRowIndex + step - 1;
            string sql = "SELECT * FROM (SELECT  ROW_NUMBER() OVER(ORDER BY MessageID ) AS number, * FROM MessageView where ConversationID =" + ConversationID + " ) AS TempTable WHERE number BETWEEN @startRowIndex AND @endRowIndex";
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);

            cmd.Parameters.AddWithValue("@startRowIndex", startRowIndex);
            cmd.Parameters.AddWithValue("@endRowIndex", endRowIndex);
            DataTable dt = _connectData.GetDataTable(cmd);
            con.Close();
            return dt;
        }

        /// <summary>
        /// get total number of message of a conversation
        /// </summary>
        /// <param name="productID"></param>
        /// <returns>int</returns>
        public global::System.Nullable<int> TotalMessageOfOneConversation(String ConversationID)
        {

            SqlConnection con = _connectData.GetConnect();

            con.Open();

            string sql = "SELECT COUNT(*)FROM [Message] WHERE ConversationID =" + ConversationID;
            SqlCommand cmd = new SqlCommand(sql, con);

            object returnValue;

            returnValue = cmd.ExecuteScalar();

            if (((returnValue == null) || (returnValue.GetType() == typeof(global::System.DBNull))))
            {
                return new global::System.Nullable<int>();
            }
            else
            {
                return new global::System.Nullable<int>(((int)(returnValue)));
            }
        }


        /// <summary>
        /// update content of a message of a member in conversation
        /// </summary>
        /// <param name="commentID"></param>
        /// <param name="contentComment"></param>
        /// <returns>true if success</returns>
        public bool ContentMessageUpdate(String messageID, String messageContent)
        {
            SqlConnection con = _connectData.GetConnect();
            
            try
            {
                string sql = "update [Message] set MessageContent = @MessageContent where MessageID = @MessageID";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@MessageContent", messageContent);
                cmd.Parameters.AddWithValue("@MessageID", messageID);
                con.Open();
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                con.Close();

            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);
                return false;
            }
            return true;
        }
    }
}